package com.net.wanjian.networkhospitalmanager.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.wanjian.networkhospitalmanager.R;
import com.net.wanjian.networkhospitalmanager.adapter.base.RecyclerBaseAdapter;
import com.net.wanjian.networkhospitalmanager.bean.SignResultBean;
import com.net.wanjian.networkhospitalmanager.utils.URLDecoderUtil;
import com.net.wanjian.networkhospitalmanager.view.RecyclerViewX;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Xia_焱 on 2017/8/22.
 */

public class SignTimeAdapterX extends RecyclerBaseAdapter<SignResultBean.DataBean.SignInOutRecordListBean, SignTimeAdapterX.ViewHolderX> {
    int height = 0;

    public SignTimeAdapterX(Context context) {
        super(context);
    }

    @Override
    public ViewHolderX onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_sign_recycler_list, parent, false);
        return new ViewHolderX(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolderX holder, SignResultBean.DataBean.SignInOutRecordListBean item, int position) {
        final int Indexes = position;
        List<String> signOutTime = item.getSignOutTime();
        if (signOutTime.size() == 0) {
            holder.mTvSingClose.setText("");
            holder.mViewX.setVisibility(View.GONE);
            holder.mTimeClock.setVisibility(View.GONE);
            holder.imgSignOut.setVisibility(View.GONE);
            holder.tvSingOut.setVisibility(View.GONE);
        } else {
            holder.mTvSingClose.setText(URLDecoderUtil.getDecoder(item.getSignOutTime().get(0)));
            holder.mViewX.setVisibility(View.VISIBLE);
            holder.mTimeClock.setVisibility(View.VISIBLE);
            holder.imgSignOut.setVisibility(View.VISIBLE);
            holder.tvSingOut.setVisibility(View.VISIBLE);
        }


        final LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        holder.mRecyclerViewSign.setLayoutManager(layoutManager);
        List<String> strings = item.getSignInTime();
        final ArrayList<String> signInTime = new ArrayList<>();
        signInTime.addAll(strings);
        SignTimeItemAdapterX signTimeItemAdapter = new SignTimeItemAdapterX(context);
        signTimeItemAdapter.setList(signInTime);
        holder.mRecyclerViewSign.setAdapter(signTimeItemAdapter);
        holder.mRecyclerViewSign.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                View childAt = layoutManager.getChildAt(Indexes - layoutManager.findFirstVisibleItemPosition());
                if (childAt != null) {
                    height = childAt.getHeight();
                }
                int x = height * signInTime.size();
                ViewGroup.LayoutParams params = holder.mView.getLayoutParams();
                params.height = x;
                holder.mView.setLayoutParams(params);
            }
        });
    }

    class ViewHolderX extends RecyclerView.ViewHolder {
        RecyclerViewX mRecyclerViewSign;
        View mView, mViewX;
        TextView mTvSingClose, tvSingOut;
        ImageView mTimeClock, imgSignOut;

        public ViewHolderX(View itemView) {
            super(itemView);
            mRecyclerViewSign = (RecyclerViewX) itemView.findViewById(R.id.recycler_view);
            mView = itemView.findViewById(R.id.view1);
            mViewX = itemView.findViewById(R.id.view2);
            mTvSingClose = (TextView) itemView.findViewById(R.id.tv_sing_close);
            mTimeClock = (ImageView) itemView.findViewById(R.id.time_clock);
            imgSignOut = (ImageView) itemView.findViewById(R.id.img_Sign_out);
            tvSingOut = (TextView) itemView.findViewById(R.id.tv_sing_out);
        }
    }
}
